<template>
	<view class="bottom-action-wrap fboxRow Ycenter">
		<view class="bottom-space"></view>
		<view class="bottom-action">
			<view class="fboxRow Xbetween">
				<view class="left flex1 fboxRow">
					<view class="flex1" :class="!_innerData.isCollected ? 'color-6A6A6F' : 'color-FFBC44'"  @tap="onCollect">
						<text class="bksCIcon font36" :class="!_innerData.isCollected ? 'bks-c-ziyuan16weishoucang' : 'bks-c-ziyuan17shoucang'" />
						<view class="mt8 font20">{{!_innerData.isCollected ? '未收藏' : '已收藏'}}</view>
					</view>
					<view class="color-6A6A6F flex1 font20">
						<button id='share' open-type="share" hidden='true'></button>
						<label for='share'>
							<text class="bksCIcon bks-c-ziyuan23fenxiang font36"></text>
							<view class="mt8">分享</view>
						</label>
					</view>
				</view>
				<view class="right">
					<button v-if="_innerData.isBuy || _innerData.isSpecialBuy" class="button btn-gray font28 fboxRow Xcenter Ycenter"
						@tap="onTips">
						{{ _innerData.isBuy ? '已订阅': _innerData.isSpecialBuy ? '专题已订阅':'IOS暂不支持'}}
					</button>
					<button v-else class="button fboxRow Xcenter Ycenter" :disabled="payDisabled" @tap="onPay">
						<text class="bksCIcon bks-c-ziyuan113gouwuche font40"></text>
						<text v-if="innerData.valuation == '0'" class="font28"
							style="margin-left: 17rpx">{{ innerData.price == 0 ? '免费订阅' : '立即订阅'}}</text>
						<text v-else class="font28"
							style="margin-left: 17rpx">{{ userInfo && userInfo.isVIP && innerData.vipPrice == 0  ? '免费订阅' : '立即订阅'}}</text>
					</button>
				</view>
			</view>
			<view class="space"></view>
		</view>
		<confirm-dialog :show.sync="confimShow" :content="TIP0011" btnCancelTxt="付费订阅" btnConfirmText="开通VIP" @confirm="confirmVip" @cancel="cancelTap" />
		<confirm-dialog :show.sync="specialConfim" :content="TIP0010" btnCancelTxt="去购买专题" btnConfirmText="付费订阅" @confirm="confirmSpecial" @cancel="specialBuy" />
	</view>
</template>

<script>
	import { datumCollectionView, buyDatum } from '@/api/bks.datum.js';
	import { collectAdd, collectDel } from '@/api/bks.index.js';
	import { bksFunctionInfo } from '@/api/bks.index.js'
	import { getBksInfo } from '@/static/js/bks.common.js'
	import confirmDialog from './confirm-dialog.vue'
	export default {
		components: {confirmDialog},
		props: {
			type: {
				type: [Number, String],
				default: 'datum' // 0 收藏 | 2课堂 | 3 已购课程
			},
			back: {
				type: Boolean,
				default: false
			},
			// 当前项数据
			innerData: {
				type: Object,
				default: () => {
					return {
						// "collectNum": 0,
						// "gmtCreate": "",
						// "img": "",
						// "introduction": "",
						// "isCollected": false,
						// "price": 0,
						// "remark": "",
						// "specialSubjectId": "",
						// "specialSubjectName": "",
						// "valuation": "",
						// "viewNum": 0,
						// "vipPrice": 0
					}
				}
			}
		},
		computed:{
			_innerData:{
				get(){
					return this.innerData
				},
				set(val) {
					this.$emit('update:innerData', val)
				}
			}
		},
		data() {
			return {
				payDisabled: false,
				sys: {},
				confimShow: false,
				specialConfim: false,
				special: false,
				TIP0010: '',
				TIP0011: '',
				userInfo: {}
			}
		},
		mounted(){
			this.getTips()
		},
		methods: {
			getTips() {
				bksFunctionInfo({ identify: 'TIP0010' }).then(res => {
					this.TIP0010 = res.data && res.data.briefIntroduction || '专题购买提示'
				})
				bksFunctionInfo({ identify: 'TIP0011' }).then(res => {
					this.TIP0011 = res.data && res.data.briefIntroduction || 'VIP购买提示'
				})
			},
			// 收藏
			onCollect(e) {
				const { materialsId, specialSubjectId} = this._innerData
				let fn = this._innerData.isCollected ? collectDel : collectAdd
				// 收藏接口
				fn({
					businessId: this.type != 'topic' ? materialsId : specialSubjectId,
					collectType: this.type != 'topic' ? 4 : 6
				}).then(res => {
					const title = this._innerData.isCollected ? '已取消' : '已收藏'
					this._innerData.isCollected = !this._innerData.isCollected
					uni.showToast({
						title,
						icon: 'none',
						duration: 2000
					})
				}).catch(err => {
					uni.showToast({
						title: err.msg,
						icon: 'none',
						duration: 2000
					})
				})
			},
			// 已购买和iOS不支持提示
			onTips() {
				const {
					sys: {
						isOnline,
						platform
					}
				} = this
				let title = '已购买'
				if (isOnline && platform == 'ios') {
					title = 'IOS暂不支持'
				}
				wx.showToast({
					title: title,
					icon: 'none',
					duration: 2000
				})
			},
			// 订阅
			onPay() {
				const { sys } = this
				const {
					_innerData: {
						valuation,
						vipPrice,
						isSpecialBuy,
						price,
						specialSubjectId
					},
					type
				} = this
				const bw_bks_params = uni.getStorageSync('bw_bks_params') || {}
				if (bw_bks_params && bw_bks_params.access_token) {
					if (isSpecialBuy) {
						// 专题已购买
						this.payDisabled = true
						this.payment()
					} else if (valuation == 1) {
						// vip 计价
						getBksInfo('isVip').then(data=>{
							// vip 单独计价
							if (data.isVip && vipPrice == 0) {
								this.payDisabled = true
								this.payment()
							} else {
								// 资料绑定了专题提示去购买专题
								if (type == 'datum' && specialSubjectId) {
									this.specialConfim = true
								} else if (!data.isVip) {
									this.confimShow = true
								} else {
									this.payDisabled = true
									this.payment()
								}
							}
						})
					} else {
						// 不是vip计价
						if (type == 'datum' && specialSubjectId && price != 0) {
							this.specialConfim = true
						} else {
							this.payDisabled = true
							this.payment()
						}
					}
				} else {
					// 未登录跳转登录
					this.confimShow = false
					this.payDisabled = false
					this.$pageTo('/bks/login/index/index')
					getApp().globalData.buyStatus = true
				}
			},
			// 付费订阅
			confirmSpecial(){
				this.payment()
			},
			// 跳转至VIP购买
			confirmVip() {
				this.$pageTo('/bks/mine/vip/index')
			},
			// 取消和直接购买
			cancelTap(e) {
				console.log(e)
				if (e.tag == 'cancel') {
					this.payment()
				} else {
					getApp().globalData.buyStatus = true
					this.confimShow = false
					this.payDisabled = false
				}
			},
			specialBuy(e) {
				if (e.tag == 'cancel') {
					if(this.back){
						this.$pageTo('/bks/datum/topic/index', { id: this._innerData.specialSubjectId })
					}else{
						uni.navigateBack({ delta: 1 })
					}
				} else {
					getApp().globalData.buyStatus = true
					this.specialConfim = false
					this.payDisabled = false
				}
			},
			// 支付
			payment() {
				const {
					type,
					_innerData: {
						materialsId,
						specialSubjectId
					}
				} = this
				const special = this.special
				const params = {
					specialSubjectId
				}
				if (type !== 'topic') {
					params.materialsId = materialsId
				}
				buyDatum(params).then(res => {
					if (res.data.status != 'false') {
						try {
							const sys = uni.getSystemInfoSync()
							if (sys.platform == 'ios') {
								//  || this.sys.platform == 'devtools'
								uni.showToast({
									title: '由于相关规范，iOS功能暂不可用',
									icon: 'none',
									duration: 2000
								})
								this.payDisabled = false
							} else {
								const {
									weChat: {
										appId,
										timeStamp,
										nonceStr,
										packageValue,
										signType,
										paySign
									}
								} = res.data
								const _this = this
								wx.requestPayment({
									timeStamp,
									nonceStr,
									package: packageValue,
									signType,
									paySign,
									appId,
									success(res) {
										uni.showLoading({
											title: '确认订单中',
											mask: true
										})
										uni.hideLoading({
											success: () => {
												_this.innerData.isBuy = true
												_this.$emit('buyCallback', true)
												uni.showToast({
													title: '购买成功',
													icon: 'none',
													duration: 2000
												})
												_this.payDisabled = false
											}
										})
									},
									fail(res) {
										getApp().globalData.buyStatus = true
										_this.payDisabled = false
									}
								})
							}
						} catch (e) {
							//TODO handle the exception
						}
					} else {
						if (!res.data.weChat) {
							this._innerData.isBuy = true
							uni.showToast({
								title: '已订阅，请点击查看',
								icon: 'none',
								duration: 2000
							})
						} else {
							uni.showToast({
								title: '购买失败',
								icon: 'none',
								duration: 2000
							})
						}
						this.$emit('buyCallback', true)
						this.payDisabled = false
						getApp().globalData.buyStatus = true
					}
				}).catch(err => {
					this.payDisabled = false
					getApp().globalData.buyStatus = true
				})
			},
		}
	}
</script>

<style lang="scss" scoped>

.bottom-space { width: 100%; height: calc(100rpx + env(safe-area-inset-bottom));}
.space{ width: 100%;padding-bottom:calc(2rpx + env(safe-area-inset-bottom));}
.bottom-action {
  width: 100%;
  background: #FFFFFF;
  border-top: 1px solid #F0F0F5;
  position: fixed;
  bottom: 0;
  padding: 13rpx 30rpx;
  text-align: center;
  z-index: 999;
}

.bottom-action .right .button {
  width: 400rpx;
  height: 68rpx;
  text-align: center;
  line-height: 68rpx;
  background: #FF9645;
  border-radius: 4rpx;
  color: #FFFFFF;
}
.bottom-action .right .btn-gray{background: #DFE0ED;}
</style>